
#include "communicatUart3Rx.h"

#include "baseData.h"
#include "uart.h"
 
static uint16_t rxUart3Index = 0;
static uint16_t txUart3Index = 0; 
uint8_t g_DetectTimeOpen=0;


void uart3RecieveUpdate(void)
{
    txUart3Index = getRemainRecv3Buf();
    if(rxUart3Index != txUart3Index)				
    {												
        int16_t len = txUart3Index - rxUart3Index; 	
        if (len < 0)
        {
            len += 256;
        }
        uint16_t i = 0;
        while (i < len)
        {
            uint16_t index = rxUart3Index + i;
            i++;
            if (index > (256-1))
            {
                index -= 256;
            }
            uart3RecieveHandle(g_rx3[index]);
        }
        rxUart3Index = txUart3Index;
    }
}

void uart3RecieveHandle(uint8_t data) 
{									
    static uint8_t status; 			
    static uint8_t cksum;	 		
    static uint8_t bufIndex;		
    static uint8_t buf[115];		
    switch (status)					
	{								
		case 0:						
			status = (data == 0xFD) ? 1 : 0; 
			break;							 
		case 1:								 
			status = (data == 0xFD) ? 2 : 0; 
			bufIndex = 0;
			cksum=0;
			break;
		case 2:
			buf[bufIndex] = data;
			bufIndex++;
			cksum += data;
			if (bufIndex == 114) 
			{
				status = 3; 
			} 
			break;
		case 3:  
			if(cksum == data) /*所有校验正确*/
			{									
				uart3Recieved(buf);					
				if((buf[0]==0xfe)&&(buf[1]==0xfe))
				{									
					g_DetectTimeOpen=1;				
				}
			}
			status = 0;
			cksum=0;
			break; 		
		default:	
			break;
    }
}

/***
*一帧数据一共收到0xfd+0xfd+37*6+check=2+222+1=225个字节
*
*/
void uart3Recieved(uint8_t *data)
{
	for(uint8_t i=0;i<108;i++)//30个数据+1个check
	{
		From_to.Usart3_to_Usart1[i+6]=data[i];
	}
}


